/**
 * Copyright 2009 - Morten Udnæs
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.javabatch.petstore.process;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.javabatch.fileio.FieldError;
import com.javabatch.fileio.FileBuffer;
import com.javabatch.fileio.FileStore;
import com.javabatch.fileio.RecordBasedFileStore;
import com.javabatch.petstore.records.AnimalRecord;
import com.javabatch.petstore.records.CatalogRecord;
import com.javabatch.petstore.records.PriceRecord;
import com.javabatch.petstore.records.Record;


public class CreateCatalogFileProcess implements BatchProcess {
	private FileStore animalStore;
	private FileStore catalogStore;
	
	public CreateCatalogFileProcess(String priceFile, String catalogFile) {
		animalStore = new RecordBasedFileStore(priceFile, RecordBasedFileStore.READ_ACCESS);
		catalogStore = new RecordBasedFileStore(catalogFile, RecordBasedFileStore.WRITE_ACCESS,
		CatalogRecord.getLengthAndValidateRecord());
	}

	@Override
	public List execute(List<Record> records) {
		Random rnd = new Random();
		List<Integer> results = new ArrayList<Integer>();
		List<FieldError> errors = new ArrayList<FieldError>();
		Integer animalId = new Integer(0);
		Random r = new Random();
		animalStore.open();
		catalogStore.open();
		
		//First record contains headers
		FileBuffer animalBuffer = null;
		
		while (animalStore.hasMore()) {
			animalId++;
			animalBuffer = animalStore.readNext();
			
			FileBuffer catalogBuffer = new FileBuffer();
			
			CatalogRecord.CATALOG_ID.write(catalogBuffer, animalId.toString());
			CatalogRecord.ANIMAL_ID.write(catalogBuffer, 
					AnimalRecord.ANIMAL_GUID.read(animalBuffer, errors));
			
			
			String desc = AnimalRecord.ANIMAL_NAME.read(animalBuffer, errors);
			
			if (desc.length() > 40)
				desc = desc.substring(0,40);
			
			CatalogRecord.DESCRIPTION.write(catalogBuffer, desc); 
					
			CatalogRecord.PRICE.write(catalogBuffer,Integer.toString(r.nextInt(1000)));
			
			catalogStore.write(catalogBuffer);
			
			if (errors.size() > 0 ) {
				throw new RuntimeException("Something went wrong!");
			}
			
			if (animalId % 10000 == 0) {
				System.out.println("Current inventoryID: "+ animalId);
			}
		}
		animalStore.close();
		catalogStore.close();
		results.add(animalId);
		
		return results;
		
	}

	@Override
	public List<Object> prepare(List<Record> records) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void release(List<Record> records) {
		// TODO Auto-generated method stub
		
	}

}
